#include "Forest.h"

Forest::Forest(const Forest& Fcopy)
{
	int i=0;
	while(i<Fcopy.ForestCount())
	{
		this->AddRightTree(Fcopy.getTree(i++));
	}
}

void Forest::AddRightTree(Tree* T) 
{
	if(T!=NULL)
		Frst.push_back(T);
}

Tree* Forest::GetRightTree() const 
{
	if(Frst.size()!=0)
		return Frst.back();
	else
		return NULL;
}

Forest Forest::GetRightRootlessTree() const
{
	Forest out;
	if(Frst.size()!=0)
	{
		Tree* temp=Frst.back();
		if(temp->getRight()!=NULL)
			out.AddLeftTree(temp->getRight());
		if(temp->getLeft()!=NULL)
			out.AddLeftTree(temp->getLeft());
	}
	return out;
}

void Forest::RemoveRightTree()
{
	if(Frst.size()!=0)
		Frst.pop_back();
}

void Forest::RemoveRightRoot()
{
	if(Frst.size()!=0)
	{
		Tree* temp=Frst.back();
		Frst.pop_back();
		if(temp->getLeft()!=NULL)
			Frst.push_back(temp->getLeft());
		if(temp->getRight()!=NULL)
			Frst.push_back(temp->getRight());
	}
}

void Forest::AddLeftTree(Tree* T) 
{
	if(T!=NULL)
		Frst.push_front(T);
}

Tree* Forest::GetLeftTree() const 
{
	if(Frst.size()!=0)
		return Frst.front();
	else
		return NULL;
}

Forest Forest::GetLeftRootlessTree() const
{
	Forest out;
	Tree* temp=Frst.front();
	if(temp->getRight()!=NULL)
		out.AddLeftTree(temp->getRight());
	if(temp->getLeft()!=NULL)
		out.AddLeftTree(temp->getLeft());
	return out;
}

void Forest::RemoveLeftTree()
{
	if(Frst.size()!=0)
		Frst.pop_front();
}

void Forest::RemoveLeftRoot()
{
	if(Frst.size()!=0)
	{
		Tree* temp=Frst.front();
		Frst.pop_front();
		if(temp->getRight()!=NULL)
			Frst.push_front(temp->getRight());
		if(temp->getLeft()!=NULL)
			Frst.push_front(temp->getLeft());
	}
}


int Forest::ForestSize() const
{
	unsigned int sum=0;
	for(unsigned int i=0; i<Frst.size() ; i++)
	{
		sum+=Frst[i]->getSize()+1;
	}
	return sum;
}
